package jee.boot.module.mail.rest;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import jee.boot.common.basic.CrudController;
import jee.boot.common.basic.result.ResponseMessage;
import jee.boot.common.basic.result.Result;
import jee.boot.module.mail.SendMailHelper;
import jee.boot.entity.mail.Mail;
import jee.boot.module.mail.service.SysMailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 邮件发送Controller
 *
 * @author zhilei.wang
 * @version 2018-01-25
 */
@Api(tags = "邮件发送")
@RestController
@RequestMapping(value = "/mail")
public class SysMailRestController extends CrudController<SysMailService, Mail> {
    @Autowired
    private SendMailHelper helper;

    /**
     * 异步发送邮件
     *
     * @param entity
     * @return
     */
    @PostMapping("/send")
    @ApiOperation(value = "发送邮件")
    public ResponseMessage<Mail> send(@RequestBody Mail entity) {
        if (entity.isAsyn()) {
            //异步邮件,经过mq后排队发送
            helper.sendMailAsyn(entity);
        } else {
            // 同步邮件,直接发送
            helper.sendMailSync(entity);
        }
        return Result.success(entity);
    }

    /**
     * 异步发送邮件
     *
     * @param entity
     * @return
     */
    @PostMapping("/sendCalendarMail")
    @ApiOperation(value = "发送邮件")
    public ResponseMessage<Mail> sendCalendarMail(@RequestBody Mail entity) {
        // 同步邮件,直接发送日历类邮件
        helper.sendCalendarMailSync(entity);

        // 返回结果有信息
        return Result.success(entity);
    }
}
